package models

import "github.com/beego/beego/v2/client/orm"

type ErpAcceptance struct {
	Id             int64
	DepartmentId   int64
	Entry          *ErpEntry `orm:"rel(fk);null;on_delete(do_nothing)"`
	Number         string
	AcceptanceUser string
	AcceptanceNum  int64 //验收数量
	AcceptanceDate int64
}

func (a *ErpAcceptance) TableName() string {
	return ErpAcceptanceTableName()
}
func ErpAcceptanceTableName() string {
	return TableName("erp_acceptance")
}

//查询
type ErpAcceptanceQueryParam struct {
	BaseQueryParam
	EntryNumber  string `json:"entry_number"`
	DepartmentId int64  `json:"depid"`
}

func ErpAcceptancePageList(params *ErpAcceptanceQueryParam) ([]*ErpAcceptance, int64) {

	ls := make([]*ErpAcceptance, 0)

	query := orm.NewOrm().QueryTable(ErpAcceptanceTableName()).RelatedSel("Entry", "Entry__Suppliers")

	if len(params.EntryNumber) > 0 {
		query = query.Filter("Entry__number", params.EntryNumber)
	}
	query = query.Filter("department_id", params.DepartmentId)

	sortorder := "-Id"

	switch params.Sort {
	case "Id":
		sortorder = "Id"
	}
	if params.Order == "desc" {
		sortorder = "-" + sortorder
	}

	total, _ := query.Count()

	query.OrderBy(sortorder).Limit(params.Limit, params.Offset).All(&ls)

	return ls, total
}
